
#include <BLabel>
#include <BButton>
#include <BStyle>

#include "FileDialog.h"

FileDialog::FileDialog(BWidget* parent) : BFileDialog(parent)
{
	_pathComboBox = (BComboBox*)this->find("path");
	_pathComboBox->setEditable(true);
	BWidget* treeWidget = this->find("tree");
	BWidget* tableWidget = this->find("table");

	_buttonLayout = new BHBoxLayout(_pathComboBox);

	_menu = new BMenu();
	BAction* openFolderAction = _menu->addAction("Open");
	BAction* removeAction = _menu->addAction("Remove");
	BAction* createFileAction = _menu->addAction("Create File");
	BAction* createFolderAction = _menu->addAction("Create Folder");
	BAction* propertyAction = _menu->addAction("Property");

	connect(_pathComboBox, Signal_TextChanged, &FileDialog::slotPathTextChanged);
	connect(_pathComboBox, Signal_Focused, &FileDialog::slotPathWidgetFocused);
	connect(_pathComboBox, Signal_Freshed, &FileDialog::slotPathComboBoxFreshed);
	connect(treeWidget, Signal_RightClicked, &FileDialog::slotTreeWidgetRightClicked);
	connect(tableWidget, Signal_RightClicked, &FileDialog::slotTableWidgetRightClicked);
}
FileDialog::~FileDialog()
{

}

void FileDialog::freshButtons()
{
	const BString& text = _pathComboBox->text();
	BStringArray strlst = text.split('/');
	if (text.beginWith('/'))
		strlst.prepend("/");
	for (int i = 0; i < _buttonLayout->count(); i++)
	{
		BObject* object = _buttonLayout->object(i);
		disconnect(object, Signal_Clicked);
	}
	_buttonLayout->host()->clearChildren();
	_buttonLayout->clear();
	for (int i = 0; i < strlst.size() - 1; i++)
	{
		PathButton* button = new PathButton();
		button->setText(strlst[i]);
		button->setWidthPolicy(Policy_Preferred);
		_buttonLayout->addWidget(button);
		connect(button, Signal_Clicked, &FileDialog::slotPathButtonClicked);
	}
	BLabel* label = new BLabel(strlst.last());
	label->setWidthPolicy(Policy_Preferred);
	label->setMargin(Part_Left, 6);
	_buttonLayout->addWidget(label);
	_buttonLayout->addSpring();
}

void FileDialog::slotPathTextChanged(BObject* object, const BValue& value)
{
	freshButtons();
}
void FileDialog::slotPathWidgetFocused(BObject* object, const BValue& value)
{
	if (value == true)
	{
		_buttonLayout->setVisible(false);
		_pathComboBox->setForm(Form_IconText);
	}
	else
	{
		_buttonLayout->setVisible(true);
		_pathComboBox->setForm(Form_IconOnly);
	}
}
void FileDialog::slotPathComboBoxFreshed(BObject* object, const BValue& value)
{
	BRect iconRect = _pathComboBox->query("icon-rect");
	_buttonLayout->setMargin(Part_Left, iconRect.width());
}
void FileDialog::slotPathButtonClicked(BObject* object, const BValue& value)
{
	BString path;
	for (int i = 0; i < _buttonLayout->count(); i++)
	{
		BButton* button = (BButton*)_buttonLayout->widget(i);
		path << button->text() << '/';
		if (button == object)
			break;
	}
	this->setPath(path);
}
void FileDialog::slotTreeWidgetRightClicked(BObject* object, const BValue& value)
{

}
void FileDialog::slotTableWidgetRightClicked(BObject* object, const BValue& value)
{

}

void FileDialog::styleEvent(const BEvent& event)
{
	BFileDialog::styleEvent(event);
	const BStyle* style = this->realStyle();
	if (const BStyle* titleStyle = style->annex("title"))
	{
		const BValue& titleHeightValue = titleStyle->value(Value_Height);
		if (titleHeightValue.valid())
		{
			int titleHeight = titleHeightValue;
		}
	}
}
